package com.hiultra.archive.controller;

import java.net.URISyntaxException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.hiultra.archive.domain.ArchiveDesTask;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hiultra.common.annotation.Log;
import com.hiultra.common.core.controller.BaseController;
import com.hiultra.common.core.domain.AjaxResult;
import com.hiultra.common.enums.BusinessType;
import com.hiultra.archive.domain.ArchiveUploadTask;
import com.hiultra.archive.service.IArchiveUploadTaskService;
import com.hiultra.common.utils.poi.ExcelUtil;
import com.hiultra.common.core.page.TableDataInfo;

/**
 * 转存任务Controller
 *
 * @author paidaxing
 * @date 2024-07-24
 */
@RestController
@RequestMapping("/archive/ArchiveUploadTask")
public class ArchiveUploadTaskController extends BaseController {
    @Autowired
    private IArchiveUploadTaskService archiveUploadTaskService;

    /**
     * 查询转存任务列表
     */
    @PreAuthorize("@ss.hasPermi('archive:ArchiveUploadTask:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArchiveUploadTask archiveUploadTask) {
        startPage();
        List<ArchiveUploadTask> list = archiveUploadTaskService.selectArchiveUploadTaskList(archiveUploadTask);
        return getDataTable(list);
    }

    /**
     * 导出转存任务列表
     */
    @PreAuthorize("@ss.hasPermi('archive:ArchiveUploadTask:export')")
    @Log(title = "转存任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArchiveUploadTask archiveUploadTask) {
        List<ArchiveUploadTask> list = archiveUploadTaskService.selectArchiveUploadTaskList(archiveUploadTask);
        ExcelUtil<ArchiveUploadTask> util = new ExcelUtil<ArchiveUploadTask>(ArchiveUploadTask.class);
        util.exportExcel(response, list, "转存任务数据");
    }

    /**
     * 获取转存任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('archive:ArchiveUploadTask:query')")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") String taskId) {
        return AjaxResult.success(archiveUploadTaskService.selectArchiveUploadTaskByTaskId(taskId));
    }

    /**
     * 新增转存任务
     */
    @PreAuthorize("@ss.hasPermi('archive:ArchiveUploadTask:add')")
    @Log(title = "转存任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArchiveUploadTask archiveUploadTask) {
        return toAjax(archiveUploadTaskService.insertArchiveUploadTask(archiveUploadTask));
    }

    /**
     * 修改转存任务
     */
    @PreAuthorize("@ss.hasPermi('archive:ArchiveUploadTask:edit')")
    @Log(title = "转存任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArchiveUploadTask archiveUploadTask) {
        return toAjax(archiveUploadTaskService.updateArchiveUploadTask(archiveUploadTask));
    }

    /**
     * 删除转存任务
     */
    @PreAuthorize("@ss.hasPermi('archive:ArchiveUploadTask:remove')")
    @Log(title = "转存任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable String[] taskIds) {
        return toAjax(archiveUploadTaskService.deleteArchiveUploadTaskByTaskIds(taskIds));
    }

    /**
     * 转存任务结果上报接口
     */
    @ApiOperation("转存任务结果上报接口")
    @PostMapping("/archiveUploadTaskResultReport")
    @PreAuthorize("@ss.hasPermi('archive:ArchiveUploadTask:archiveUploadTaskResultReport')")
    public AjaxResult archiveUploadTaskResultReport(@RequestBody ArchiveUploadTask archiveUploadTask) throws JsonProcessingException, URISyntaxException {
        if (null == archiveUploadTask) {
            return AjaxResult.error("转存任务信息为空，请检查重试！");
        }
        if (null == archiveUploadTask.getTransferNo()) {
            return AjaxResult.error("工单信息为空，请检查重试！");
        }
        return AjaxResult.success(archiveUploadTaskService.archiveUploadTaskResultReport(archiveUploadTask));
    }
}
